%%%-------------------------------------------------------------------
%%% @doc  
%%% Copyright (C) 2007, Lucas Carlson
%%% File    : drproxy_client.erl
%%% Author  : Lucas Carlson (lucas@rufy.com)
%%% @end
%%%-------------------------------------------------------------------
-module(drproxy_client).

-behaviour(application).
%%--------------------------------------------------------------------
%% Include files
%%--------------------------------------------------------------------
-include("drproxy_client.hrl").

%%--------------------------------------------------------------------
%% External exports
%%--------------------------------------------------------------------
-export([
	 start/2, shutdown/0, stop/1, config_file/0, defaults/1,
	 config/1, servers/0, client_port/1, pid_file/1, resource_pid/1,
	 status/0
	 ]).

%%--------------------------------------------------------------------
%% Macros
%%--------------------------------------------------------------------

%%--------------------------------------------------------------------
%% Records
%%--------------------------------------------------------------------

%%====================================================================
%% External functions
%%====================================================================
%%--------------------------------------------------------------------
%% @doc The starting point for an erlang application.
%% @spec start(Type, StartArgs) -> {ok, Pid} | {ok, Pid, State} | {error, Reason}
%% @end
%%--------------------------------------------------------------------
start(Type, StartArgs) ->
    case dpc_sup:start_link(StartArgs) of
	{ok, Pid} -> 
	    {ok, Pid};
	Error ->
	    Error
    end.

%%--------------------------------------------------------------------
%% @doc Called to shudown the drproxy_client application.
%% @spec shutdown() -> ok 
%% @end
%%--------------------------------------------------------------------
shutdown() ->
    application:stop(drproxy_client).

config_file() ->
	case application:get_env(drproxy_client, config) of
		{ok, Config} ->
			Config;
		_ ->
			error_logger:error_msg("The config file has not been specified~n"),
			exit({config, missing_config_file})
	end.

defaults(Type) ->
	Defaults = [
		{concurrency, 0},
		{delay, 0},
		{check_times, 3},
		{startup_delay, 20000},
		{monitor_frequency, infinity},
		{restart_after_completed, infinity},
		{client_resources, 1},
		{client_spares, 0},
		{hot_swap_every, 1},
		{servers, [node()]},
		{client_first_port, 3000},
		{response_timeout, 60000},
		{max_cpu, 10},
		{max_ram, 5},
		{start_script, "/usr/local/bin/ruby /usr/local/bin/mongrel_rails start -d -e production -p ~w -l log/mongrel.log -P log/mongrel.~w.pid -c /usr/local/sites/www/current -r public -S config/mongrel_stats.conf"},
		{base_directory, "/usr/local/sites/www/current"},
		{pid_file, "/usr/local/sites/www/current/log/mongrel.~w.pid"}
	],
	case lists:keysearch(Type, 1, Defaults) of
		{value, {Type, Value}} ->
			Value;
		false ->
			error_logger:error_msg("default value not found for key ~w~n", [Type]),
			exit({default_value, not_found})
	end.

config(Type) ->
	Dict = utils:config(config_file()),
	case catch dict:fetch(Type, Dict) of
		{'EXIT', _} ->
			defaults(Type);
		R ->
			R
	end.

servers() ->
	config(servers).

client_port(N) ->
	config(client_first_port)+N-1.

pid_file(N) ->
	lists:flatten(io_lib:format(config(pid_file), [client_port(N)])).

resource_pid(N) ->
	CatCmd = lists:flatten(io_lib:format("cat ~s", [pid_file(N)])),
	Cat = os:cmd(CatCmd),
	list_to_integer(string:substr(Cat, 0, length(Cat)-1)).

status() ->
	dpc_server:status(),
	resource_server:status(),
	process_server:status().

%%====================================================================
%% Internal functions
%%====================================================================

%%--------------------------------------------------------------------
%% Called upon the termintion of an application.
%%--------------------------------------------------------------------
stop(State) ->
    ok.

